import { client } from '../../request/index.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
Page({
  data: {
    ip: getApp().globalData.ip,
    imgIp: getApp().globalData.imgIp,
    exchange_company_information: [],
    acount: [],
    allCompanyInfo: [],
    inputValue: '',
    total: [],
    isResEmptyHidden: true,
    widthChange: false,
    navbarInitTop: 0, //导航栏初始化距顶部的距离
    isFixedTop: false, //是否固定顶部
  },
  //获取查询店铺输入
  handleSearchInput(e) {
    let inputValue = e.detail.value
    this.setData({
      inputValue,
    })
    if (inputValue === '') {
      this.handleReset()
    }
  },
  //点击商家
  handleQueryTap(e) {
    const eid = e.currentTarget.dataset.eid
    const ename = e.currentTarget.dataset.ename
    wx.navigateTo({
      url: 'shop/shop?eid=' + eid + '&ename=' + ename,
    })
  },
  //获取店铺信息
  async getStore() {
    var that = this
    const getData = await client({
      url: '/enterprise/queryallenterprise',
    })
    console.log('店铺信息', getData)
    //统计商品数
    const getData1 = await client({
      url: '/enterprise/countMerchandises',
    })
    console.log('统计商品数', getData1)
    this.setData({
      exchange_company_information: getData.data.data,
      allCompanyInfo: getData.data.data,
      acount: getData1.data.data,
    })
    for (let index2 in that.data.acount) {
      that.data.total.push(that.data.acount[index2].total)
    }
    var totalarray = that.data.total
    for (let index1 in this.data.exchange_company_information) {
      var total = 'exchange_company_information[' + index1 + '].total'
      this.setData({
        [total]: totalarray[index1],
      })
    }
  },

  //根据店铺名称查询
  async fromName(inputValue) {
    let that = this
    const getData = await client({
      url: '/enterprise/queryenterprise',
      data: {
        ename: inputValue,
      },
    })
    //统计商品数
    const getData1 = await client({
      url: '/enterprise/countMerchandises',
    })
    console.log('搜索结果', getData)
    if (
      getData.data.code === 888 &&
      getData.data.message === '没有搜索到相关店铺'
    ) {
      this.setData({
        isResEmptyHidden: false,
      })
    } else {
      Toast.success('搜索成功')
      this.setData({
        exchange_company_information: getData.data.data,
        acount: getData1.data.data,
      })
      for (let index2 in that.data.acount) {
        that.data.total.push(that.data.acount[index2].total)
      }
      var totalarray = that.data.total
      for (let index1 in this.data.exchange_company_information) {
        var total = 'exchange_company_information[' + index1 + '].total'
        this.setData({
          [total]: totalarray[index1],
        })
      }
    }
  },
  // 查询搜索的接口方法
  handleSearch() {
    let that = this
    let inputValue = this.data.inputValue
    if (inputValue === '') {
      Toast.fail('输入不能为空')
      return
    }
    that.fromName(inputValue)
  },

  handleReset() {
    this.setData({
      isResEmptyHidden: true,
      inputValue: '',
    })
    this.getStore()
  },

  onLoad: function (options) {
    this.getStore()
  },

  onShow: function () {
    var that = this
    if (that.data.navbarInitTop == 0) {
      //获取节点距离顶部的距离
      wx.createSelectorQuery()
        .select('#Topbar')
        .boundingClientRect(function (rect) {
          if (rect && rect.top > 0) {
            var navbarInitTop = parseInt(rect.top)
            that.setData({
              navbarInitTop: navbarInitTop,
            })
          }
        })
        .exec()
    }
  },

  onPageScroll: function (e) {
    var that = this
    var scrollTop = parseInt(e.scrollTop) //滚动条距离顶部高度
    //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
    var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false
    //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
    if (that.data.isFixedTop === isSatisfy) {
      return false
    }
    that.setData({
      isFixedTop: isSatisfy,
      widthChange: isSatisfy,
    })
  },

  onPullDownRefresh: function () {
    this.setData({
      isResEmptyHidden: true,
      inputValue: '',
    })
    this.getStore()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },
})
